<html>
<head><meta charset="utf-8"><title>const parameter math possible · project-const-generics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/index.html">project-const-generics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const.20parameter.20math.20possible.html">const parameter math possible</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="225925224"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const%20parameter%20math%20possible/near/225925224" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> noname <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const.20parameter.20math.20possible.html#225925224">(Feb 10 2021 at 23:01)</a>:</h4>
<p>Is it possible to perform const parameter match inside an asociated function??<br>
I'm trying to do a compile time allocation of resources and this type of function fails with the <code>use of generic parameter from outer function</code> error.<br>
Example:</p>
<div class="codehilite"><pre><span></span><code>pub struct Resource&lt;const N: u32, const A: u32&gt;;

impl&lt;const N:u32, const A: u32&gt; Resource&lt;N, A&gt; {
    pub fn read(&amp;self) {
        read_offset( { A + (2 * N) } )
    }
}
</code></pre></div>



<a name="225926383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const%20parameter%20math%20possible/near/225926383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const.20parameter.20math.20possible.html#225926383">(Feb 10 2021 at 23:11)</a>:</h4>
<p>your example seems fine?</p>



<a name="225926408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const%20parameter%20math%20possible/near/225926408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const.20parameter.20math.20possible.html#225926408">(Feb 10 2021 at 23:11)</a>:</h4>
<p>ignoring that <code>read_offset</code> does not exist, it compiles as expected?</p>



<a name="225931979"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const%20parameter%20math%20possible/near/225931979" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> noname <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const.20parameter.20math.20possible.html#225931979">(Feb 11 2021 at 00:06)</a>:</h4>
<p>Ooops, yeah, didn't realise what actually caused the problem was assigning those values to constants for better reading. Apparently that's what breaks it? Is there a fix for that?? Still, thanks for the help</p>



<a name="225935322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const%20parameter%20math%20possible/near/225935322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jubilee <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const.20parameter.20math.20possible.html#225935322">(Feb 11 2021 at 00:51)</a>:</h4>
<p>consts that depend on const generics can easily cause things to throw a fit, that I have noticed.</p>



<a name="225965488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const%20parameter%20math%20possible/near/225965488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const.20parameter.20math.20possible.html#225965488">(Feb 11 2021 at 09:23)</a>:</h4>
<p>free consts cannot depend on generic parameters from outer functions, that's true</p>



<a name="225965558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const%20parameter%20math%20possible/near/225965558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const.20parameter.20math.20possible.html#225965558">(Feb 11 2021 at 09:24)</a>:</h4>
<p>so the following causes a compiler error</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">FREE</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">size_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="225966304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/const%20parameter%20math%20possible/near/225966304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/const.20parameter.20math.20possible.html#225966304">(Feb 11 2021 at 09:31)</a>:</h4>
<p>you can use an associated constant instead, so the following works:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">Resource</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">u32</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">A</span>: <span class="kt">u32</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>:<span class="kt">u32</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="n">A</span>: <span class="kt">u32</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Resource</span><span class="o">&lt;</span><span class="n">N</span><span class="p">,</span><span class="w"> </span><span class="n">A</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">ASSOC</span>: <span class="kt">u32</span> <span class="o">=</span><span class="w"> </span><span class="n">A</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="p">(</span><span class="mi">2</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">N</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">read</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">read_offset</span><span class="p">(</span><span class="bp">Self</span>::<span class="n">ASSOC</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>